public class QuickSort {
    public static void quickSort(int[] array){
        quick(array,0, array.length-1);
    }

    private static void quick(int[] array, int start, int end) {
        if (start > end) {
            return;
        }

        int index = middleNum(array,start,end);
        swap(array,index,start);

        int pivot = partitionHoare(array,start,end);
        swap(array,start,pivot-1);
        swap(array,pivot+1,end);
    }

    private static int middleNum(int[] array,int left,int right) {
        int mid = left+(right-left >> 1);
        if (array[left] < array[right]) {
            if (array[mid] < array[left]) {
                return left;
            } else if (array[right] < array[mid]) {
                return right;
            }else {
                return mid;
            }
        }else {
            if(array[mid] < array[right]) {
                return right;
            }else if(array[mid] > array[left]) {
                return left;
            }else {
                return mid;
            }
        }
    }

    private static int partitionHoare(int[] array,int left,int right) {
        int tmp = array[left];//基准
        int i = left;
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            while (left < right && array[left] <= tmp) {
                left++;
            }
            swap(array,left,right);
        }
        swap(array,i,left);
        return left;
    }

    private static void swap(int[] array, int i, int k) {
        int tmp = array[i];
        array[i] = array[k];
        array[k] = tmp;
    }
}
